﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ProjectN.Core.JsonSerialization
{
    public static class SerializationHelper
    {
        private static readonly JsonSerializer defaultSerializer = JsonSerializer.Create(General.DefaultSettings);
        public static void WriteToStream<T>(Stream stream, T obj, JsonSerializer? serializer = null) where T : class
        {
            var currentSerializer = serializer ?? defaultSerializer;

            using var writer = new StreamWriter(stream);
            {
                currentSerializer.Serialize(writer, obj);
                writer.Flush();
            }
        }
        public static T? ReadFromStream<T>(Stream stream, JsonSerializer? serializer = null) where T : class
        {
            var currentSerializer = serializer ?? defaultSerializer;

            using var reader = new StreamReader(stream);
            {
                return currentSerializer.Deserialize(reader, typeof(T)) as T;
            }
        }
    }
}
